我正在使用knitr创建一个包含多个图像的markdown文件。因为我不想每次都说出这条路,所以我希望它是相对的。
mainDir <- ~/Documents/R/Images/
subDir <- Logo
color <- blue
现在我想添加'.png'来打印实际图像。而且我不想将颜色更改为<- blue.png
,因为我在代码的其他部分使用变量'blue'。
img1 <- rasterGrob(as.raster(readPNG(file.path(mainDir, subDir, color, ".png"))), interpolate = FALSE)
我收到以下错误: readPNG中的错误(file.path(mainDir,subDir,img,“。png”)): 无法打开/Documents/R/Images/Logo/blue/.png
如何删除最后一个“/”,以便它是'blue.png'而不是blue / .png?
答案 0 :(得分:2)
怎么样:
<div id="dnnStandardMenu">
<ul class="dnnNav">
<li data-page-id="676387" class="root mi mi0 sel first" style="width: 126px;"><a href="/Default.aspx?tabid=676387" target="" class="txt">Home</a></li>
<li data-page-id="914191" class="root mi mi0" style="width: 144px;"><a href="/Default.aspx?tabid=914191" target="" class="txt">Culture</a></li>
<li data-page-id="914192" class="root mi mi0" style="width: 145px;"><a href="/Default.aspx?tabid=914192" target="" class="txt">Contact</a></li>
<li data-page-id="914193" class="root mi mi0" style="width: 132px;"><a href="/Default.aspx?tabid=914193" target="" class="txt">Coach</a></li>
<li data-page-id="914194" class="root mi mi0 haschild" style="width: 138px;"><a href="/Default.aspx?tabid=914194" target="" class="txt">REFEREE</a><ul class="m m0">
<li class="col"><ul class="col1"><li data-page-id="914196" class="mi mi1 first"><a href="/Default.aspx?tabid=914196" target="" class="txt"><span class="icn no-icn"></span>Online Training</a></li><li data-page-id="914198" class="mi mi1 last"><a href="/Default.aspx?tabid=914198" target="" class="txt"><span class="icn no-icn"></span>Referee Supplies</a></li></ul></li><li class="col"><ul class="col2"><li data-page-id="914197" class="mi mi1"><a href="/Default.aspx?tabid=914197" target="" class="txt"><span class="icn no-icn"></span>SAY vs. USSF Rule Differences</a></li></ul></li><li class="col"><ul class="col3"><li data-page-id="914199" class="mi mi1"><a href="/Default.aspx?tabid=914199" target="" class="txt"><span class="icn no-icn"></span>Qualifications</a></li></ul></li></ul>
</li>
<li data-page-id="914205" class="root mi mi0" style="width: 138px;"><a href="/Default.aspx?tabid=914205" target="" class="txt">sadmin</a></li>
<li data-page-id="7" class="root mi mi0 haschild last" style="width: 123px;"><span class="txt">Host</span><ul class="m m0" style="visibility: visible; left: -55px; width: 752px; right: auto; display: none;">
<li class="col" style="height: 260px;"><ul class="col1"><li data-page-id="187309" class="mi mi1 first"><a href="/Default.aspx?tabid=187309&portalid=9048" target="" class="txt"><span class="icn no-icn"></span>Health Monitor</a></li><li data-page-id="252866" class="mi mi1"><a href="/Default.aspx?tabid=252866&portalid=9048" target="" class="txt"><span class="icn no-icn"></span>Host Alerts</a></li><li data-page-id="116842" class="mi mi1"><a href="/Default.aspx?tabid=116842&portalid=9048" target="" class="txt"><span class="icn no-icn"></span>Payment Sync</a></li><li data-page-id="85608" class="mi mi1"><a href="/Default.aspx?tabid=85608&portalid=9048" target="" class="txt"><span class="icn no-icn"></span>Report Wizard 2.0</a></li><li data-page-id="19" class="mi mi1"><a href="/Default.aspx?tabid=19&portalid=9048" target="" class="txt"><span class="icn no-icn"></span>File Manager</a></li><li data-page-id="83444" class="mi mi1"><a href="/Default.aspx?tabid=83444&portalid=9048" target="" class="txt"><span class="icn no-icn"></span>Lists</a></li><li data-page-id="112125" class="mi mi1"><a href="/Default.aspx?tabid=112125&portalid=9048" target="" class="txt"><span class="icn no-icn"></span>Extensions</a></li><li data-page-id="545857" class="mi mi1 last"><a href="/Default.aspx?tabid=545857&portalid=9048" target="" class="txt"><span class="icn no-icn"></span>HTML Editor Manager</a></li></ul></li><li class="col" style="height: 260px;"><ul class="col2"><li data-page-id="16" class="mi mi1"><a href="/Default.aspx?tabid=16&portalid=9048" target="" class="txt"><span class="icn no-icn"></span>Host Settings</a></li><li data-page-id="181364" class="mi mi1"><a href="/Default.aspx?tabid=181364&portalid=9048" target="" class="txt"><span class="icn no-icn"></span>Bulk Email</a></li><li data-page-id="92782" class="mi mi1"><a href="/Default.aspx?tabid=92782&portalid=9048" target="" class="txt"><span class="icn no-icn"></span>Daily Payment Summary</a></li><li data-page-id="129517" class="mi mi1"><span class="txt"><span class="icn no-icn"></span>Store</span><ul class="m m1">
<li data-page-id="129518" class="mi mi2 first"><a href="/Default.aspx?tabid=129518&portalid=9048" target="" class="txt"><span class="icn no-icn"></span>Royalty Invoices</a></li>
<li data-page-id="129519" class="mi mi2"><a href="/Default.aspx?tabid=129519&portalid=9048" target="" class="txt"><span class="icn no-icn"></span>Royalty Report - Current Period</a></li>
<li data-page-id="149762" class="mi mi2"><a href="/Default.aspx?tabid=149762&portalid=9048" target="" class="txt"><span class="icn no-icn"></span>Product Sales Report</a></li>
<li data-page-id="152479" class="mi mi2"><a href="/Default.aspx?tabid=152479&portalid=9048" target="" class="txt"><span class="icn no-icn"></span>Product Sales Origin Report</a></li>
<li data-page-id="157549" class="mi mi2"><a href="/Default.aspx?tabid=157549&portalid=9048" target="" class="txt"><span class="icn no-icn"></span>Portal Products</a></li>
<li data-page-id="157550" class="mi mi2 last"><a href="/Default.aspx?tabid=157550&portalid=9048" target="" class="txt"><span class="icn no-icn"></span>Product Attribute Editor</a></li>
</ul>
</li><li data-page-id="21" class="mi mi1"><a href="/Default.aspx?tabid=21&portalid=9048" target="" class="txt"><span class="icn no-icn"></span>SQL</a></li><li data-page-id="83445" class="mi mi1"><a href="/Default.aspx?tabid=83445&portalid=9048" target="" class="txt"><span class="icn no-icn"></span>Superuser Accounts</a></li><li data-page-id="113401" class="mi mi1"><a href="/Default.aspx?tabid=113401&portalid=9048" target="" class="txt"><span class="icn no-icn"></span>Skins</a></li></ul></li><li class="col last" style="height: 260px;"><ul class="col3"><li data-page-id="308159" class="mi mi1"><a href="/Default.aspx?tabid=308159&portalid=9048" target="" class="txt"><span class="icn no-icn"></span>Order Summary</a></li><li data-page-id="17" class="mi mi1"><a href="/Default.aspx?tabid=17&portalid=9048" target="" class="txt"><span class="icn no-icn"></span>Site Management</a></li><li data-page-id="93184" class="mi mi1"><a href="/Default.aspx?tabid=93184&portalid=9048" target="" class="txt"><span class="icn no-icn"></span>Invoice Summary</a></li><li data-page-id="157553" class="mi mi1"><a href="/Default.aspx?tabid=157553&portalid=9048" target="" class="txt"><span class="icn no-icn"></span>AT</a><ul class="m m1">
<li data-page-id="157557" class="mi mi2 first"><a href="/Default.aspx?tabid=157557&portalid=9048" target="" class="txt"><span class="icn no-icn"></span>Template Manager</a></li>
<li data-page-id="157558" class="mi mi2 last"><a href="/Default.aspx?tabid=157558&portalid=9048" target="" class="txt"><span class="icn no-icn"></span>Email Template Manager</a></li>
</ul>
</li><li data-page-id="3810" class="mi mi1"><a href="/Default.aspx?tabid=3810&portalid=9048" target="" class="txt"><span class="icn no-icn"></span>Schedule</a></li><li data-page-id="112124" class="mi mi1"><a href="/Default.aspx?tabid=112124&portalid=9048" target="" class="txt"><span class="icn no-icn"></span>Dashboard</a></li><li data-page-id="545856" class="mi mi1"><a href="/Default.aspx?tabid=545856&portalid=9048" target="" class="txt"><span class="icn no-icn"></span>Configuration Manager</a></li></ul></li></ul>
</li>
</ul>
</div>
结果: &#34;〜/文档/ R /图像/标志/ blue.png&#34;
答案 1 :(得分:2)
您可以使用paste
函数将文件扩展名连接到路径字符串。
img1 <- rasterGrob(as.raster(readPNG(paste(file.path(mainDir, subDir, color), ".png", sep = ""))), interpolate = FALSE)
http://stat.ethz.ch/R-manual/R-patched/RHOME/library/base/html/paste.html
此外,/
上不需要尾随mainDir <- "~/Documents/R/Images"
,因为在添加子目录时它会重复。