如何在 Shiny 中编辑文件上传功能的特定部分?
How to edit specific portions of the File upload functionality in Shiny?
我对 R 编程(以及 Whosebug 也是)完全陌生,并且正在使用 Shiny 包开发一个小型 R 项目(它似乎更容易并且符合我的要求)。现在我需要上传一个 .csv 文件,Shiny 已经为其提供了模板或以 ui.R 和 server.R 的形式说出代码。但我的问题是 Shiny 提供的代码片段包含浏览部分以及 Headers 的复选框部分和用于选择分隔符(逗号、分号、制表符)和引号(None 的单选按钮双,单).
。
现在我只需要浏览部分而不是 header 或分隔符或引号所以我修改了脚本但单选按钮似乎没有。
# The code section starts here.
# Original Script provided by Shiny.
ui.R
library(shiny)
ui <- fluidPage(
titlePanel("Uploading Files"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose CSV File',
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')),
tags$hr(),
checkboxInput('header', 'Header', TRUE),
radioButtons('sep', 'Separator',
c(Comma=',',
Semicolon=';',
Tab='\t'),
','),
radioButtons('quote', 'Quote',
c(None='',
'Double Quote'='"',
'Single Quote'="'"),
'"')
),
mainPanel(
tableOutput('contents')
)
)
)
server.R file
library(shiny)
server <- function(input, output) {
output$contents <- renderTable({
# input$file1 will be NULL initially. After the user selects
# and uploads a file, it will be a data frame with 'name',
# 'size', 'type', and 'datapath' columns. The 'datapath'
# column will contain the local filenames where the data can
# be found.
inFile <- input$file1
if (is.null(inFile))
return(NULL)
read.csv(inFile$datapath, header=input$header, sep=input$sep,
quote=input$quote)
})
}
shinyApp(ui=ui, server = server)
我能够删除 header 复选框部分,如第二张图片所示
但无法删除单选框。请大家提前指教一下如何做that.Thanking
我不知道我是否正确理解了你的问题,但你可以简单地从 ui
:
中删除两个 radioButtons()
和 checkboxInput()
ui <- fluidPage(
titlePanel("Uploading Files"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose CSV File',
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')),
tags$hr()
),
mainPanel(
tableOutput('contents')
)
)
)
然后从 server
中的 read.csv()
中删除或替换三个输入变量(input$header
、input$set
和 input$quote
):
server <- function(input, output) {
output$contents <- renderTable({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
read.csv(inFile$datapath, header=T, sep=";")
})
}
我对 R 编程(以及 Whosebug 也是)完全陌生,并且正在使用 Shiny 包开发一个小型 R 项目(它似乎更容易并且符合我的要求)。现在我需要上传一个 .csv 文件,Shiny 已经为其提供了模板或以 ui.R 和 server.R 的形式说出代码。但我的问题是 Shiny 提供的代码片段包含浏览部分以及 Headers 的复选框部分和用于选择分隔符(逗号、分号、制表符)和引号(None 的单选按钮双,单).
现在我只需要浏览部分而不是 header 或分隔符或引号所以我修改了脚本但单选按钮似乎没有。
# The code section starts here.
# Original Script provided by Shiny.
ui.R
library(shiny)
ui <- fluidPage(
titlePanel("Uploading Files"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose CSV File',
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')),
tags$hr(),
checkboxInput('header', 'Header', TRUE),
radioButtons('sep', 'Separator',
c(Comma=',',
Semicolon=';',
Tab='\t'),
','),
radioButtons('quote', 'Quote',
c(None='',
'Double Quote'='"',
'Single Quote'="'"),
'"')
),
mainPanel(
tableOutput('contents')
)
)
)
server.R file
library(shiny)
server <- function(input, output) {
output$contents <- renderTable({
# input$file1 will be NULL initially. After the user selects
# and uploads a file, it will be a data frame with 'name',
# 'size', 'type', and 'datapath' columns. The 'datapath'
# column will contain the local filenames where the data can
# be found.
inFile <- input$file1
if (is.null(inFile))
return(NULL)
read.csv(inFile$datapath, header=input$header, sep=input$sep,
quote=input$quote)
})
}
shinyApp(ui=ui, server = server)
我能够删除 header 复选框部分,如第二张图片所示
但无法删除单选框。请大家提前指教一下如何做that.Thanking
我不知道我是否正确理解了你的问题,但你可以简单地从 ui
:
radioButtons()
和 checkboxInput()
ui <- fluidPage(
titlePanel("Uploading Files"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose CSV File',
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')),
tags$hr()
),
mainPanel(
tableOutput('contents')
)
)
)
然后从 server
中的 read.csv()
中删除或替换三个输入变量(input$header
、input$set
和 input$quote
):
server <- function(input, output) {
output$contents <- renderTable({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
read.csv(inFile$datapath, header=T, sep=";")
})
}