用 R shiny 改变数据帧
Altering dataframe with R shiny
我是一般编码的新手,目前正在为我的应用程序制作 R shiny 应用程序。
它的目的是
- 上传 csv 文件
- 有多个复选框。如果勾选,数据会走相应的脚本。
- 导出新数据
我已经看过该教程,但我目前在反应性方面遇到了一些困难。
我也尝试浏览其他问题,但由于我对编码不熟悉,我发现很难从他们的示例中选择我需要的内容。
我目前已正确完成导入和导出功能,并为正文编写了脚本。但是,我不确定如何将此 "body" 合并到服务器端。
这是 "body" 中的一篇,没有考虑 Shiny:
file1 <- file1[ grep("REVERSE", file1[,c(1)], fixed = FALSE, invert = TRUE),]
Ui 位于
的某处
... fileInput('file1'
.... checkboxInput(inputId = "rmDecoy",
label = "Remove Decoy Entry",
value = TRUE
),
.... mainPanel(
tableOutput('contents')
虽然这是我到目前为止写的服务器端,只有导出功能:
server <- function(input, output) {
getData <- reactive({
inFile <- input$file1
if (is.null(input$file1))
return(NULL)
read.csv(inFile$datapath, header=input$header, sep=input$sep,
quote=input$quote)
})
output$contents <- renderTable(
getData()
)
output$downloadData <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(getData(), file)
})
}
我做的时候有点用output$rmDecoy
但是当我把它和下载数据功能放在一起时,它就停止工作了。
因此,我的问题是
- 我的理解是您不是要直接更改输入。相反,您正在渲染新的 table、更改它并导出它。我了解 R shiny 的原理吗?
- 如何将上面的脚本合并到服务器中?
感谢您的帮助。
一个稍微精简的工作示例如下所示。请注意,我将您的数据操作步骤 file1 <- file1[ grep("REVERSE", file1[,c(1)], fixed = FALSE, invert = TRUE),]
替换为仅获取前两行。您也可以将此步骤移至 getData
并仅使用一个 reactive
,如果您在其他地方的应用中永远不需要未处理的数据。
希望对您有所帮助!
library(shiny)
ui <- fluidPage(
fileInput('file1','file1'),
tableOutput('table_to_show'),
downloadButton('downloadData', label = "Download")
)
server <- function(input, output) {
getData <- reactive({
inFile <- input$file1
if (is.null(input$file1))
return(NULL)
read.csv(inFile$datapath)
})
contents <- reactive({
dat<- getData()
print(dat)
# manipulations to uploaded data here.
dat <- dat[1:2,]
})
output$table_to_show <- renderTable(
contents()
)
output$downloadData <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(contents(), file)
})
}
shinyApp(ui,server)
我是一般编码的新手,目前正在为我的应用程序制作 R shiny 应用程序。 它的目的是
- 上传 csv 文件
- 有多个复选框。如果勾选,数据会走相应的脚本。
- 导出新数据
我已经看过该教程,但我目前在反应性方面遇到了一些困难。 我也尝试浏览其他问题,但由于我对编码不熟悉,我发现很难从他们的示例中选择我需要的内容。
我目前已正确完成导入和导出功能,并为正文编写了脚本。但是,我不确定如何将此 "body" 合并到服务器端。
这是 "body" 中的一篇,没有考虑 Shiny:
file1 <- file1[ grep("REVERSE", file1[,c(1)], fixed = FALSE, invert = TRUE),]
Ui 位于
的某处... fileInput('file1'
.... checkboxInput(inputId = "rmDecoy",
label = "Remove Decoy Entry",
value = TRUE
),
.... mainPanel(
tableOutput('contents')
虽然这是我到目前为止写的服务器端,只有导出功能:
server <- function(input, output) {
getData <- reactive({
inFile <- input$file1
if (is.null(input$file1))
return(NULL)
read.csv(inFile$datapath, header=input$header, sep=input$sep,
quote=input$quote)
})
output$contents <- renderTable(
getData()
)
output$downloadData <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(getData(), file)
})
}
我做的时候有点用output$rmDecoy
但是当我把它和下载数据功能放在一起时,它就停止工作了。
因此,我的问题是
- 我的理解是您不是要直接更改输入。相反,您正在渲染新的 table、更改它并导出它。我了解 R shiny 的原理吗?
- 如何将上面的脚本合并到服务器中?
感谢您的帮助。
一个稍微精简的工作示例如下所示。请注意,我将您的数据操作步骤 file1 <- file1[ grep("REVERSE", file1[,c(1)], fixed = FALSE, invert = TRUE),]
替换为仅获取前两行。您也可以将此步骤移至 getData
并仅使用一个 reactive
,如果您在其他地方的应用中永远不需要未处理的数据。
希望对您有所帮助!
library(shiny)
ui <- fluidPage(
fileInput('file1','file1'),
tableOutput('table_to_show'),
downloadButton('downloadData', label = "Download")
)
server <- function(input, output) {
getData <- reactive({
inFile <- input$file1
if (is.null(input$file1))
return(NULL)
read.csv(inFile$datapath)
})
contents <- reactive({
dat<- getData()
print(dat)
# manipulations to uploaded data here.
dat <- dat[1:2,]
})
output$table_to_show <- renderTable(
contents()
)
output$downloadData <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(contents(), file)
})
}
shinyApp(ui,server)