java文件上传过滤,当您允许用户上传文件并过滤数据时,为什么需要renderUI?

我想问一个与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)

})

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值