我想问一个与shiny包中的renderUI()相关的问题 . 我创建了一个闪亮的应用程序,它允许用户上传数据文件并过滤数据 . 基于过滤器,我的闪亮应用程序创建了一个ggplot2图形 .
Data
我的数据文件看起来像这样 . 为了给你一些想法,我留下以下代码 . 请注意,所有列都在此处 .
user_id
latitude
longitude
dates
foo
Initial attempt (failure)
我写了对开服务器.R和ui.R.它们允许我上传数据文件 . 但是,R不会过滤数据并创建图形 . 没有错误消息 .
server.R
library(shiny)
library(data.table)
library(plyr)
library(dplyr)
library(ggplot2)
shinyServer(function(input,output) {
### Let users to choose and upload a file.
dataSet
if (is.null(input$file1)) {
return(NULL)
}
fread(input$file1$datapath)
})
### Now I want to create a chunk of code which filters a date set using date information
ana
if (is.null(dataSet)){
return(NULL)
}
dataSet() %>%
filter(dates >= input$inVar2[1], dates <= input$inVar2[2]) %>%
group_by(user_id, dates) %>%
summarize(count = n())
})
output$theGraph
theGraph
geom_boxplot() +
xlab("Dates") +
ylab("Appliacation usage (times)") +
ggtitle("How many times did each user use the app each day?")
print(theGraph)
})
})
ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Test"),
sidebarLayout(
sidebarPanel(
fileInput("file1", "Choose file to upload",
accept = c(
"text/csv",
"text/comma-separated-values",
"text/tab-separated-values",
"text/plain",
".csv",
".tsv"
)),
dateRangeInput("inVar2", label = "Date range",
start = "2014-07-01", end = "2014-07-10")
),
mainPanel(plotOutput("theGraph"))
)
))
Second attempt (success)
我搜索了堆栈溢出并找到了以下两个帖子,这实际上帮我写了以前的服务器.R . 我想对贡献者表示感谢 . 有一次,我仔细阅读,我意识到我可能需要使用renderUI()来创建我想要的应用程序 . 解决方案是使用renderUI()并将dateRangeInput()放在其中 .
这两个链接
server.R
library(shiny)
library(data.table)
library(dplyr)
library(ggplot2)
shinyServer(function(input, output) {
### Let users choose and upload a file.
dataSet
if (is.null(input$file1)) {
return(NULL)
}
fread(input$file1$datapath)
})
### Let users choose a date range.
output$inVar2
if (is.null(dataSet)) {
return(NULL)
}
dateRangeInput("inVar2", label = h3("Date range"),
start = "2014-07-01", end = "2014-07-10")
})
### Based on the date range, filter the data.
ana
if (is.null(dataSet)) {
return(NULL)
}
dataSet() %>%
filter(dates >= input$inVar2[1], dates <= input$inVar2[2]) %>%
group_by(user_id, dates) %>%
summarize(count = n())
})
### Draw a graphic
output$theGraph
print(ana())
theGraph
geom_boxplot() +
ggtitle("How many times did each user\n use the app each day?") +
xlab("Dates") +
ylab("Appliacation usage\n (times)") +
theme(plot.title = element_text(size = 25, lineheight=.8, face="bold")) +
theme(axis.text.x = element_text(angle=90, color = "black", face = "bold", size = 10)) +
theme(axis.title.x = element_text(face="bold", color = "black", size=20)) +
theme(axis.title.y = element_text(face="bold", color = "black", size=20))
print(theGraph)
})
})
ui.R
shinyUI(
fluidPage(
titlePanel("Application usage summary"),
plotOutput("theGraph"),
hr(),
fluidRow(
column(3,
fileInput("file1", h3("Choose file to upload"),
accept = c(
"text/csv",
"text/comma-separated-values",
"text/tab-separated-values",
"text/plain",
".csv",
".tsv")
)),
column(3,
uiOutput("inVar2"))
)
))
我很高兴看到我的闪亮应用程序正在运行 . 但是,我想知道为什么我的第一次尝试不对 . 如果您在'shinyServer(function(input, output) {'部分之前上传文件,我认为我的第一种方法有效 . 但是,似乎无论何时允许用户上传文件,您都需要使用renderUI() .
Main question
有没有什么方法可以使用我的第一种方法(失败方法)创建我的应用程序类型?如果有方法,它们会是什么?如果没有,我想了解更多关于renderUI()的内容,此时CRAN手册中似乎需要更多信息 .
Minor question
当我运行此应用程序时,我在上传文件之前看到此错误消息 . 一旦上传数据文件,它就会消失 . (这个问题已经解决了 . 一个拼写错误导致整个剧本 . )
错误:没有适用于“过滤器”的方法应用于类“NULL”的对象
这个问题的答案是使用验证 .
dataSet
validate(
need(input$file1 != "", "Please select a data set")
)
fread(input$file1$datapath)
})